package com.correction.classes;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name = "utilisateur")
public class Utilisateur {

	public enum TypeUser {
		ADMIN, NORMAL, PROFESSEURS
	}

	public enum Niveau {
		NIVEAU1, NIVEAU2, NIVEAU3, NIVEAU4
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "idUtilisateur")
	private int idUtilisateur;

	public Niveau getNiveau() {
		return niveau;
	}

	public void setNiveau(Niveau niveau) {
		this.niveau = niveau;
	}

	public List<Exercice> getExecrices() {
		return execrices;
	}

	public void setExecrices(List<Exercice> execrices) {
		this.execrices = execrices;
	}

	@Column(name = "nom")
	private String nom;
	@Column(name = "prenom")
	private String prenom;
	@Column(name = "mail")
	private String mail;
	@Column(name = "pwd")
	private String pwd;
	@Column(name = "type")
	@Enumerated(EnumType.STRING)
	private TypeUser type;
	@Column(name = "niveau")
	@Enumerated(EnumType.STRING)
	private Niveau niveau;
	// //
	 @OneToMany(cascade = CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	@JoinColumn(name = "idUtilisateur", nullable = true)
	@ForeignKey(name = "FK_NoteId")
	private List<NoteExercice> notesExercices = new ArrayList<NoteExercice>();

	// //NoteExamens
	 @OneToMany(cascade = CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	@JoinColumn(name = "idUtilisateur", nullable = true)
	@ForeignKey(name = "FK_NoteExamId")
	private List<NoteExamen> notesExamens = new ArrayList<NoteExamen>();

	public List<NoteExercice> getNotesExercices() {
		return notesExercices;
	}

	public void setNotesExercices(List<NoteExercice> notesExercices) {
		this.notesExercices = notesExercices;
	}

	public List<NoteExamen> getNotesExamens() {
		return notesExamens;
	}

	public void setNotesExamens(List<NoteExamen> notesExamens) {
		this.notesExamens = notesExamens;
	}

	 @OneToMany(cascade = CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	@JoinColumn(name = "idUtilisateur", nullable = true)
	@ForeignKey(name = "FK_ExerciceId")
	private List<Exercice> execrices = new ArrayList<Exercice>();

//	@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
	 @OneToMany(cascade = CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	@JoinColumn(name = "idUtilisateur", nullable = true)
	@ForeignKey(name = "FK_ExamenId")
	private List<Examen> examens = new ArrayList<Examen>();

	public List<Examen> getExamens() {
		return examens;
	}

	public void setExamens(List<Examen> examens) {
		this.examens = examens;
	}

	public int getIdUtilisateur() {
		return idUtilisateur;
	}

	public void setIdUtilisateur(int idUtilisateur) {
		this.idUtilisateur = idUtilisateur;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public TypeUser getType() {
		return type;
	}

	public void setType(TypeUser type) {
		this.type = type;
	}

	public Utilisateur(String nom, String prenom, String mail, String pwd,
			TypeUser type, Niveau niveau) {
		super();
		this.nom = nom;
		this.prenom = prenom;
		this.mail = mail;
		this.pwd = pwd;
		this.type = type;
		this.niveau = niveau;
	}

	@Override
	public String toString() {
		return "Utilisateur [idUtilisateur=" + idUtilisateur + ", nom=" + nom
				+ ", prenom=" + prenom + ", mail=" + mail + ", pwd=" + pwd
				+ ", type=" + type + ", niveau=" + niveau + "]";
	}

	public Utilisateur() {
	}

	public Examen getExamenById(int id) {
		for (Examen examen : examens) {
			if (id == examen.getIdExamen())
				return examen;
		}
		return null;
	}

	public Exercice getExercice(int id) {
		for (Exercice exercice : execrices) {
			if (id == exercice.getId())
				return exercice;
		}
		return null;
	}

	public Examen getExamenByExerciceId(int id) {
		for (Examen examen : examens) {
			List<Exercice> exercices = examen.getExercices();
			for (Exercice exercice : exercices) {
				if (exercice.getId() == id)
					return examen;
			}
		}
		return null;
	}

}